package com.wisezone.springmvc.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wisezone.springmvc.entity.User;

/**
 * 
 * 接收客户端请求参数 
 *    1.request 
 *    2.方法参数； 如果参数名与表单属性不同，需要加 @RequestParam("username")
 *    3. model对象,如果参数过多
 * 
 * 
 * 响应用数据 1.request 2.Model, 3.ModelMap   4. Map<String,String>
 * 
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	// http://ip:port/food/user/show
	@RequestMapping("/show")
	public ModelAndView showMessage() {
		/*
		 * 1.接收请求 2.调用业务代码获取数据 3.响应数据
		 */
		String message = "第一个springmvc程序";
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg", message);
		mv.setViewName("message");

		return mv;
	}

	@RequestMapping("/add")
	public ModelAndView addUser() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg", "添加用户成功");
		mv.setViewName("message");
		return mv;
	}

	@RequestMapping(value = "/save")
	public String saveUser(Model model) {
		model.addAttribute("msg", "保存用户成功!"); // <=>request.setAttribute("msg","保存用户成功!");
		return "message";
	}

	// http://ip:port/food/user/save_submit
	@RequestMapping(value = "/save_submit", method = RequestMethod.POST)
	public String saveSubmit(HttpServletRequest request) {
		String userName = request.getParameter("username");
		String age = request.getParameter("age");
		System.out.println("userName :" + userName + " age :" + age);
		request.setAttribute("msg", "userName :" + userName + " age :" + age);
		return "message";
	}

	@RequestMapping(value = "/save_submit_one", method = RequestMethod.POST)
	public String saveSubmit(@RequestParam("username") String name, int age, Model model) {

		System.out.println("userName :" + name + " age :" + age);

		model.addAttribute("msg", "save_submit_one userName :" + name + " age :" + age);
		return "message";
	}

	@RequestMapping(value = "/save_submit_two", method = RequestMethod.POST)
	public String saveSubmit(User user, Model model) {
		System.out.println("userName :" + user.getUsername() + " age :" + user.getPassword());
		model.addAttribute("msg", "save_submit_two >>> : userName :" + user.getUsername() + " age :" + user.getPassword());
		return "message";
	}

	@RequestMapping(value = "/save_submit_three", method = RequestMethod.POST)
	public String saveSubmit(User user, ModelMap modelMap) {
		System.out.println("userName :" + user.getUsername() + " age :" + user.getPassword());
		modelMap.put("msg", "userName :" + user.getUsername() + " age :" + user.getPassword());
		return "message";
	}

	@RequestMapping(value = "/save_submit_three", method = RequestMethod.GET)
	public String saveSubmit(User user, Map<String,String> map) {
		System.out.println("userName :" + user.getUsername() + " age :" + user.getPassword());
		map.put("msg", "userName :" + user.getUsername() + " age :" + user.getPassword());
		return "message";
	}
}
